//
//  AppDelegate.m
//  VoiceOfHeart
//
//  Created by 吉龙 on 16/8/12.
//  Copyright © 2016年 吉龙. All rights reserved.
//

#import "AppDelegate.h"
#import "VOHFirstViewController.h"
#import <CoreLocation/CoreLocation.h>
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>

#import "VOH_AreaDic.h"
@interface AppDelegate ()<CLLocationManagerDelegate>
@property(nonatomic,strong) CLLocationManager *locationManage;


@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [self.window makeKeyAndVisible];
    self.window.rootViewController = [[VOHFirstViewController alloc] init];
    //定位
    self.locationManage = [[CLLocationManager alloc] init];
    // 设置定位精度，十米，百米，最好
    [self.locationManage setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];
    self.locationManage.delegate = self;
    self.locationManage.desiredAccuracy = kCLLocationAccuracyBest;
    self.locationManage.distanceFilter = 10;
    // 开始时时定位
    [self.locationManage startUpdatingLocation];
   
    // 设置音乐后台播放的会话类型
    AVAudioSession *session = [AVAudioSession sharedInstance];
    [session setCategory:AVAudioSessionCategoryPlayback error:nil];
    [session setActive:YES error:nil];
    
    // 开启接收远程事件
    [application beginReceivingRemoteControlEvents];
    
    
    //563ab7dbe0f55a08a40009aa
    
   // [UMSocialData setAppKey:@"563ab7dbe0f55a08a40009aa"];
    
   // [UMSocialSinaHandler openSSOWithRedirectURL:@"http://sns.whalecloud.com/sina2/callback"];
    
    return YES;
}

#pragma mark--定位代理方法
// 错误信息
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    NSLog(@"error");
}

-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
    switch (status) {
        case kCLAuthorizationStatusNotDetermined:
            if ([_locationManage respondsToSelector:@selector(requestAlwaysAuthorization)]) {
                [_locationManage requestWhenInUseAuthorization];
            }
            break;
            
        default:
            break;
    }
    
}

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
    CLLocation *newLocation = locations[0];
    //    CLLocationCoordinate2D oldCoordinate = newLocation.coordinate;
    //    NSLog(@"旧的经度：%f,旧的纬度：%f",oldCoordinate.longitude,oldCoordinate.latitude);
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder reverseGeocodeLocation:newLocation
                   completionHandler:^(NSArray *placemarks, NSError *error){
                       
                       for (CLPlacemark *place in placemarks) {
                           NSString *string = place.administrativeArea;
                           NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
                           [userDefaults setObject:string forKey:@"location"];
                           
                           VOH_AreaDic *dic = [[VOH_AreaDic alloc]init];
                           for (NSString *key in dic.areadictionary) {
                               if ([string containsString:key]) {
                                   NSString *ID = [dic.areadictionary objectForKey:key];
                                   [userDefaults setObject:ID forKey:@"CityID"];
                                   break;
                               }
                           }
                           
                       }
                       
                   }];
    
}

//- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
//{
//    return  [UMSocialSnsService handleOpenURL:url];
//}
//- (BOOL)application:(UIApplication *)application
//            openURL:(NSURL *)url
//  sourceApplication:(NSString *)sourceApplication
//         annotation:(id)annotation
//{
//    return  [UMSocialSnsService handleOpenURL:url];
//}
- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
